package com.example.dangerouscargodriver.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.entry.OrderStatusInfo;
import com.example.dangerouscargodriver.ui.activity.MyOrderDetailInfoActivity;

import java.util.ArrayList;
import java.util.List;

public class OrderInfoLoadMoreAdapter extends LoadMoreAdapter{

	private static final String TAG = "OrderInfoAdapter";
	private List<OrderStatusInfo> mOrderInfoItemList = null;
	private Context mContext;
	private View mRootView;
	private ClickListener mClickListener;
	private OnContentListener mContentListener = null;

	public interface OnContentListener{
		void onLoadMore();
	}

	static class ViewHolder extends CommonRcViewHolder {
		private TextView mFromName = null;
		private TextView mToName = null;
		private TextView mSourceDistance = null;
		private TextView mRegTime = null;

		public ViewHolder(View view) {
			super(view);
			mFromName = view.findViewById(R.id.item_start_city);
			mSourceDistance = view.findViewById(R.id.item_source_distance);
			mToName = view.findViewById(R.id.item_end_city);
			mRegTime = view.findViewById(R.id.item_register_time);
		}

	}

	public OrderInfoLoadMoreAdapter() {
		mOrderInfoItemList = new ArrayList<>();
		mClickListener = new ClickListener();
	}

	public void setContentListener(OnContentListener mContentListener) {
		this.mContentListener = mContentListener;
	}

	@Override
	public int getFooterViewResId() {
		return R.layout.item_footer;
	}

	@Override
	public int getFooterTextViewResId() {
		return R.id.tv;
	}

	@Override
	public int getCount() {
		if(mOrderInfoItemList != null) {
			return mOrderInfoItemList.size();
		}
		else{
			return 0;
		}
	}

	@Override
	public CommonRcViewHolder getViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_resource_info_item,
				parent,
				false);
		return new ViewHolder(view);
	}

	@Override
	public void loadData(CommonRcViewHolder holder, int position) {
		final int selectPos = position;
		OrderStatusInfo resourceInfo = mOrderInfoItemList.get(position);
		ViewHolder viewHolder = (ViewHolder) holder;
		Log.d(TAG, "resourceInfo: " + resourceInfo.toString());
		viewHolder.mFromName.setText(resourceInfo.getOrder_sg_from_city());
		viewHolder.mToName.setText(resourceInfo.getOrder_sg_to_city());
		viewHolder.mSourceDistance.setText(resourceInfo.getOrder_sg_distance());
		viewHolder.mRegTime.setText(resourceInfo.getOrder_createtime());

		holder.itemView.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				mClickListener.onClick(selectPos);
				notifyDataSetChanged();
			}
		});
	}

	public void appendData(List<OrderStatusInfo> array) {
		for(int index = 0; index < array.size(); index++){
			mOrderInfoItemList.add(array.get(index));
		}
		notifyDataSetChanged();
	}

	@Override
	public void loadMore() {
		if(mContentListener != null){
			mContentListener.onLoadMore();
		}
	}

	public class ClickListener {
		private int mPosition = -1;

		void onClick(int position){
			mPosition = position;
			Intent intent = new Intent(mRootView.getContext(), MyOrderDetailInfoActivity.class);
			intent.putExtra("ItemIndex", mPosition);
			mRootView.getContext().startActivity(intent);
		}

		public int getPosition() {

			return mPosition;
		}

		public void setPosition(int mPosition) {

			this.mPosition = mPosition;
		}
	}
}
